package cn.com.oceansoft.business.os.service.impl;

import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.entity.Dept;
import cn.com.oceansoft.business.os.entity.Lucky;
import cn.com.oceansoft.business.os.entity.User;
import cn.com.oceansoft.business.os.persistence.ILuckyMapper;
import cn.com.oceansoft.business.os.service.ILuckyService;
import cn.com.oceansoft.business.sys.entity.SysResource;
import cn.com.oceansoft.business.sys.entity.SysRole;
import cn.com.oceansoft.nos.common.spring.SequenceGenerator;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangxl on 2016/12/20.
 */

@Service
public class LuckyServiceImpl implements ILuckyService {

    @Resource
    private SequenceGenerator sequenceGenerator;

    @Resource
    private ILuckyMapper luckyMapper;

    @Override
    public Pager<Lucky> findPageByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        return PageUtil.page2pager((Page<Lucky>) luckyMapper.findPageByCondition(query), query);
    }

    @Override
    @Transactional
    public Result save(Lucky lucky) {
        int id = luckyMapper.save(lucky);
        if (0 < id) {
            lucky.setId(id);
            return new Result(Result.SUCC, lucky);
        } else {
            return new Result(Result.FAILURE, "奖项新增失败！");
        }
    }

    @Override
    @Transactional
    public Result update(Lucky lucky) {
        int id = luckyMapper.update(lucky);
        if (1 == id) {
            return new Result(Result.SUCC, lucky);
        } else {
            return new Result(Result.FAILURE, "奖项更新失败！");
        }
    }

    @Override
    public Lucky findById(int luckyId) {
        return luckyMapper.findById(luckyId);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void deleteLuckyById(int luckyId) {
        luckyMapper.deleteLuckyById(luckyId);
    }

    @Override
    public List<User> findLuckyPersonByLuckyId(int luckyId) {
        return luckyMapper.findLuckyPersonByLuckyId(luckyId);
    }

    @Override
    @Transactional
    public void updateLuckyPersonBatch( int luckyId, int[] userIds) {
//        Map<String, Object> param = new HashMap<String, Object>() {{
//            put("luckyId", luckyId);
//            put("userIds", userIds);
//        }};
        //删除指定ID的奖项对应的人
        luckyMapper.deleteLuckyPersonByLuckyId(luckyId);
        if (userIds.length > 0) {
//            List<User> list = new ArrayList<User>(userIds.length);
            for (int userId : userIds) {
//                list.add(new User(sequenceGenerator.nextUserIdSeq(), luckyId, userId));
                luckyMapper.saveLuckyPerson(luckyId,userId);
            }
//            批量插入关联信息
//            luckyMapper.saveLuckyPersonByBatch(list);
        }
    }

    @Override
    public List<Dept> findDept() {
        return luckyMapper.findDept();
    }

    @Override
    public List<User> check4User(int luckyId) {
        return luckyMapper.check4User(luckyId);
    }

    @Override
    public List<User> findLuckyBoyById(int luckyId) {
        return luckyMapper.findLuckyBoyById(luckyId);
    }

    @Override
    public Lucky findLuckyById(int luckyId) {
        return luckyMapper.findLuckyById(luckyId);
    }


}
